package com.crk.yiyuanguahao.controller;

import com.crk.yiyuanguahao.entity.Org;
import com.crk.yiyuanguahao.service.OrgService;
import com.crk.yiyuanguahao.util.LayUiTableJson;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/3/26
 */
@RestController
@RequestMapping("org")
public class OrgController {
    @Autowired
    OrgService orgService;

    @RequestMapping("manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/org/orgList.html");
        return modelAndView;
    }

    @GetMapping("detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/org/orgAdd.html");
        return modelAndView;
    }

    @RequestMapping("saveOrg")
    public ResponseResult saveOrg(Org org){
        return orgService.saveOrg(org);
    }

    @RequestMapping("deleteOrg")
    public ResponseResult deleteOrg(@RequestParam("id") Integer id){
        return orgService.deleteOrg(id);
    }

    @RequestMapping("getOrg")
    public ResponseResult getOrg(@RequestParam("id") Integer id){
        return orgService.getOrg(id);
    }

    @RequestMapping("queryOrgPage")
    public LayUiTableJson queryDoctorPage(String orgName, @RequestParam(defaultValue = "1") int page
            , @RequestParam(defaultValue = "10") int limit){
        Page pageInfo = new Page();
        pageInfo.setCurrentPage(page);
        pageInfo.setSize(limit);
        ResponseResult ResponseResult = orgService.queryOrgPage(orgName,pageInfo);
        LayUiTableJson layUiTableJson = new LayUiTableJson(0,null,pageInfo.getAllRow(),(List)ResponseResult.getData());
        return layUiTableJson;
    }

    @RequestMapping("queryOrgListTree")
    public LayUiTableJson queryOrgListTree(){
        try {
            List<Org> orgList = orgService.queryOrgAll();
            List<HashMap> data = new ArrayList<HashMap>();
            for (Org org : orgList) {
                HashMap map = new HashMap();
                map.put("id",org.getId());
                map.put("name",org.getOrgName());
                map.put("pId",org.getParentOrg());
                map.put("open",null);
                map.put("chkDisabled","false");
                data.add(map);
            }
            return new LayUiTableJson(0,null,data.size(),data);
        }catch (Exception e){
            e.printStackTrace();
            return new LayUiTableJson(1,"查询失败",0,null);
        }


    }

    @RequestMapping("getOrgList")
    public ResponseResult getOrgList(){
        List<Org> orgList = orgService.queryOrgAll();
        return ResponseResult.success(orgList);
    }

    @RequestMapping("getOrgChildrenList")
    public ResponseResult getOrgChildrenList(){
        return orgService.getOrgChildrenList();
    }

}
